#!/usr/bin/env python3
#coding: utf-8

import logging
import sys

class ExecuteLog:
    execute_log: logging.Logger

    def __init__(self, name: str, logfile=None) -> None:
        formatter = logging.Formatter('[%(asctime)s] [%(name)s] [%(levelname) .1s] - %(message)s')
        formatter.datefmt = '%Y-%m-%d %H:%M:%S'
        self.execute_log = logging.getLogger(name)
        self.execute_log.setLevel(logging.DEBUG)
        if logfile and isinstance(logfile, str):
            # print in file
            file_handler = logging.FileHandler(logfile)
            file_handler.setLevel(logging.DEBUG)
            file_handler.setFormatter(formatter)
            self.execute_log.addHandler(file_handler)
        else:
            # print in stdout
            console_handler = logging.StreamHandler(sys.stdout)
            console_handler.setLevel(logging.DEBUG)
            console_handler.setFormatter(formatter)
            self.execute_log.addHandler(console_handler)

    def info(self, str: str) -> None:
        self.execute_log.info(str)

    def error(self, str: str) -> None:
        self.execute_log.error(str)

server_logger = ExecuteLog("server")
       
